package com.abso.mp3tunes.locker.ui.properties;

import java.util.Iterator;
import java.util.List;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;

import com.abso.mp3tunes.locker.core.data.Playlist;

/**
 * A specialized section showing the properties of a playlist.
 */
public class PlaylistPropertySection extends AbstractLockerPropertySection {

	/** The text control used to show the playlist's title. */
	private Text titleText;

	/** The text control used to show the total number of tracks. */
	private Text trackCountText;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#createControls(org.eclipse.swt.widgets.Composite,
	 *      org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage)
	 */
	public void createControls(Composite parent,
			TabbedPropertySheetPage aTabbedPropertySheetPage) {
		super.createControls(parent, aTabbedPropertySheetPage);
		Composite mainComposite = createMainControl(parent);
		createCLabel(mainComposite, "Title:");
		titleText = createText(mainComposite, false);
		createCLabel(mainComposite, "Nr. of Tracks:");
		trackCountText = createText(mainComposite, false);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#refresh()
	 */
	public void refresh() {
		List elems = getSelectedElements();
		if (elems.size() == 1) {
			Playlist playlist = (Playlist) elems.get(0);
			titleText.setText(playlist.getTitle());
			trackCountText.setText(Integer.toString(playlist.getFileCount()));
		} else {
			int trackCount = 0;
			for (Iterator i = elems.iterator(); i.hasNext();) {
				Playlist playlist = (Playlist) i.next();
				trackCount += playlist.getFileCount();
			}
			titleText.setText(elems.size() + " playlists");
			trackCountText.setText(Integer.toString(trackCount));
		}
	}

}
